//
//  ViewController.swift
//  NakedEye3D
//
//  Created by 李成明 on 2022/3/30.
//

import UIKit
import SnapKit
import CoreMotion

var motionManager: CMMotionManager = {
    let manager = CMMotionManager()
    manager.deviceMotionUpdateInterval = 0.001
    return manager
}()

class ViewController: UIViewController {
    
    var backImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "banner1"))
//        view.contentMode = .scaleAspectFill
        return view
    }()
    
    var midImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "banner2"))
        view.contentMode = .scaleAspectFill
        return view
    }()
    
    var frontImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "banner3"))
        view.contentMode = .scaleAspectFill
        return view
    }()
    
    var frontImageViewCenter: CGPoint = .zero {
        didSet {
            frontImg.center = frontImageViewCenter
        }
    }
    
    var backImageViewCenter: CGPoint = .zero {
        didSet {
            backImg.center = backImageViewCenter
        }
    }
    
    /// 图片最大偏移量
    var maxOffset: CGFloat = 30
    lazy var viewCenter: CGPoint = {
        let point = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height/2)
        return point
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(backImg)
        view.addSubview(midImg)
        view.addSubview(frontImg)
        
        let imgFrame = CGRect(x: -maxOffset, y: -maxOffset, width: view.frame.size.width + maxOffset*2, height: view.frame.size.height + maxOffset*2)
        backImg.frame = imgFrame
        midImg.frame = imgFrame
        frontImg.frame = imgFrame
        
        startMotion()
    }

    private func update(gravityX: Double, gravityY: Double, gravityZ: Double) {
        print("x-->\(gravityX)\nY--->\(gravityY)\nZ--->\(gravityZ)");
        // X -->  -0.5->0.5    y: 0.2 -> -0.8  上下偏移最多达到30像素
        
        var centerOffsetX = 0.0
        var centerOffsetY = 0.0
        
        if(gravityX < -0.5) {
            centerOffsetX = -maxOffset
        } else if(gravityY > 0.5) {
            centerOffsetX = maxOffset
        } else {
            centerOffsetX = (gravityX + 0.5) * maxOffset*2 - maxOffset
        }
        
        if(gravityY < -0.8) {
            centerOffsetY = maxOffset
        } else if(gravityY > 0.2) {
            centerOffsetY = -maxOffset
        } else {
            centerOffsetY = (0.2 - gravityY) * maxOffset*2 - maxOffset
        }
        
        frontImageViewCenter = CGPoint(x: viewCenter.x + centerOffsetX, y: viewCenter.y + centerOffsetY)
        backImageViewCenter = CGPoint(x: viewCenter.x-centerOffsetX, y: viewCenter.y-centerOffsetY)
    }

    private func startMotion() {
        // 检查是否可用
        guard motionManager.isDeviceMotionAvailable else { return }
        
        motionManager.startDeviceMotionUpdates(to: OperationQueue.main) { [weak self] deviceMotion, error in
            guard let motion = deviceMotion else { return  }
            // motion.gravity 为重力方向
            self?.update(gravityX: motion.gravity.x, gravityY: motion.gravity.y, gravityZ: motion.gravity.z)

        }
    }
}

